using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using Fishes.Objects;

namespace Fishes
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        public static Tank Tank
        {
            get { return _tank; }
        }
        private static Tank _tank = new Tank();

        public static Toolbar Toolbar
        {
            get { return _toolbar; }
        }
        private static Toolbar _toolbar = new Toolbar();

        public static Random Random
        {
            get { return Game1._random; }
        }
        private static Random _random = new Random((int)DateTime.Now.Ticks);

        public static SpriteFont GameFont
        {
            get { return _gameFont; }
        }
        private static SpriteFont _gameFont = null;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            this.IsMouseVisible = true;

            if (!Game1.Tank.Load("tanks/main.tank", this.GraphicsDevice))
                throw new InvalidOperationException("Failed to load tank: tanks/main.tank");

            if (!Game1.Toolbar.Load("toolbar/toolbar.toolbar", this.GraphicsDevice))
                throw new InvalidOperationException("Failed to load toolbar: toolbar/toolbar.toolbar");

            this.Window.Title = Game1.Tank.Name;

            // set the screen to that of the tank
            this.graphics.PreferredBackBufferWidth = (int)Game1.Tank.Size.X;
            this.graphics.PreferredBackBufferHeight = (int)Game1.Tank.Size.Y + (int)Game1.Toolbar.Size.Y;
            this.graphics.ApplyChanges();

            Game1._gameFont = this.Content.Load<SpriteFont>("fonts/Kootenay");

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            if (!Game1.Tank.Destroy())
                throw new InvalidOperationException("Failed to destroy tank");

            if (!Game1.Toolbar.Destroy())
                throw new InvalidOperationException("Failed to destroy toolbar");
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if(Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();

            if (Mouse.GetState().LeftButton == ButtonState.Pressed)
            {
                Vector2 mousePos = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);

                if (mousePos.Y > Game1.Tank.Size.Y)
                {
                    Game1.Toolbar.OnMouseClick(mousePos);
                }
                else
                {
                    Game1.Tank.OnMouseClick(mousePos);
                }
            }
            else if (Mouse.GetState().LeftButton == ButtonState.Released)
            {
                Vector2 mousePos = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);

                if (mousePos.Y > Game1.Tank.Size.Y)
                {
                    Game1.Toolbar.OnMouseUp(this);
                }
                else
                {
                    Game1.Tank.OnMouseUp();
                }
            }

            Game1.Tank.Update(gameTime);

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            this.spriteBatch.Begin(SpriteBlendMode.AlphaBlend);

            Game1.Tank.Render(this.spriteBatch);
            Game1.Toolbar.Render(this.spriteBatch);

            this.spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
